new Vue({
    el:'#main',
    data:()=>({
        requestUrl:'',
        requestType:'self_fetching',
        page:1,
        pageSize:10,
        pageSum:0,
        searchText:'',
        list:[],
        checkViewData:{
            companyInfo:{},
            co_branch_info:{},
            logistorCompany:{}
        },
        logistorPickupData:{
            companyInfo:{},
            co_branch_info:{},
        },
        deliveryman:[],
        deliveryman_ids:[],
        thisAreaDeliveryData:[],
        selfFetchingOKData:{
            companyInfo:{},
            co_branch_info:{},
        },
        listIndexArr:[],
        thisAreaDeliveryDataFooter:{
            ctn:0,
            kgs:0,
            m3:0
        },
        thisAreaAndCityOKData:{
            companyInfo:{},
            co_branch_info:{},
        },
        logistorNoteData:{
            companyInfo:{},
            co_branch_info:{},
        },
        logistorCompany_id:0,
        logistorLinkAndAttr:{
            link:[],
            attr:[],
        },
        logistorCompany:[],
        batchSave:false,
        areaOrcity:'self',

        sellfCount:0,
        areaCount:0,
        cityfCount:0,
        logistorfCount:0,
        deliveryCount:0,
        finishCount:0,
        is_selfWork:0,
        searchText:'',
        isdisabled:false,
        auth_ids:$('#adminauth_ids').text()
    }),
    methods:{
        formatUnit:formatUnit,
        getdata(){
            $('.loding').show()
            $('.alert').hide()
            var data={
                request:'requestAjax',
                requestType:this.requestType,
                page:this.page,
                pageSize:this.pageSize,
                is_selfWork:this.is_selfWork,
                searchText:this.searchText,
            }
            $.ajax({
                url:this.requestUrl+'list',
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    this.list=data.data.list
                    if(data.data.pageSum){
                        this.pageSum = data.data.pageSum
                        this.Resetnav(data.data.page-0,data.data.pageSum)
                    }
                    $('.loding').hide()
                }
            })
            this.cueCount()
        },
        Resetnav(pageCurrent, pageSum) {
            //数据总条数
            var myPageCount = pageSum;
            //一页显示多少条
            var myPageSize = this.pageSize;
            //总页数
            var countindex = myPageCount % myPageSize > 0 ? (myPageCount / myPageSize) + 1 : (myPageCount / myPageSize);
            $.jqPaginator('#pagination', {
                totalPages: parseInt(countindex),
                //最多显示多少页
                visiblePages: 5,
                //默认显示第几页
                currentPage: pageCurrent,
                first: '<li class="first"><a href="javascript:;">'+langcon.page_home+'</a></li>',
                prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>'+langcon.previous_page+'</a></li>',
                next: '<li class="next"><a href="javascript:;">'+langcon.next_page+'<i class="arrow arrow3"></i></a></li>',
                last: '<li class="last"><a href="javascript:;">'+langcon.last_page+'</a></li>',
                page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
                all:'<li class="page"><a href="javascript:;">'+myPageCount+' / '+this.page+'PG , '+this.pageSize+' /PG</a></li>',
                onPageChange:(num, type)=>{
                    if (type == "change") {
                        this.page=num
                        this.getdata()
                    }
                }
            });
        },
        // 自取 -- 查看
        checkView(listDeta){
            this.checkViewData = listDeta
            if(listDeta.delivery_type_id == 4 && listDeta.trans_id > 0 ){
                this.logistorCompany_id = listDeta.trans_id
                this.getLogistorCompany('list','getLogistorCompany')
            }
            console.log(this.checkViewData)
        },
        // 自取 -- 取货
        selfLogistorPickup(listDeta,type='save'){
            $('#logistor_takeModal .alert').hide()
            if(type == 'getDeliveryman'){
                this.deliveryman_ids = []
                this.logistorPickupData = listDeta
                this.getDeliveryman('selfLogistorPickup')
            } else if(type == 'save'){
                if(!this.allInternDeliveryman()) return false
                this.pickup('selfLogistorPickup')
            }
        },
        // 代发货(本区) -- 送货
        thisAreaDelivery(listDeta,type){
            if(type == 'view'){
                this.deliveryman_ids = []
                this.thisAreaDeliveryData = []
                this.thisAreaDeliveryData.push( listDeta)
                this.thisAreaDeliveryDataFooter = {
                    ctn:listDeta.ctn,
                    kgs:listDeta.volume,
                    m3:listDeta.salePackKgs,
                } 
                this.getDeliveryman('thisAreaDelivery')
            }else if(type == 'save'){
                // 本区送货人数安排，小于1.5CBM安排2人，每加1.5CBM加多1人。必须安排1个队长。超过5个要2个队长。
                // 本市送货人数安排，最多3个人。必须安排1个司机
                // console.log(this.deliveryman_ids)
                $('#logistor_deliveryModal .alert').hide()
                var deliveryman_idsLenth = this.deliveryman_ids.length
                if(deliveryman_idsLenth >= 2){
                    if(this.thisAreaDeliveryDataFooter.m3 <= 1.5){
                        // 小于1.5CBM安排2人
                        if(deliveryman_idsLenth > 2){
                            this.point({status:2,msg:'当前最多只能选择2个送货人员'},'#logistor_deliveryModal')
                            return false
                        }else{
                            // 必须安排1个队长
                            if(this.ifIsset(37) == 0){
                                this.point({status:2,msg:'请选择一个队长人选'},'#logistor_deliveryModal')
                                return false
                            }
                        }
                    }else{
                        // 小于1.5CBM安排2人，每加1.5CBM加多1人
                        var deliveryman_idsMax = 2 + Math.floor(this.thisAreaDeliveryDataFooter.m3/1.5)-1 
                        if(deliveryman_idsLenth > deliveryman_idsMax){
                           this.point({status:2,msg:'当前最多只能选择'+deliveryman_idsMax+'个送货人员'},'#logistor_deliveryModal')
                            return false 
                        }else{
                            // 必须安排1个队长
                            if(this.ifIsset(37) == 0){
                                this.point({status:2,msg:'请选择一个队长人选'},'#logistor_deliveryModal')
                                return false
                            }else{
                                // 超过5个要2个队长。
                                if(deliveryman_idsLenth >= 5 ){
                                    if(this.ifIsset(37) < 2){
                                        this.point({status:2,msg:'当前需要请选择2个队长人选'},'#logistor_deliveryModal')
                                        return false
                                    }
                                }
                            }
                        }
                    }    
                }else{
                    this.point({status:2,msg:'请选择送货人员，不低于2个送货人员'},'#logistor_deliveryModal')
                    return false
                }
                this.thisAreaAndCitySave('thisAreaDelivery')
            }
            
        },

        // 批量送货
        batchDelivery(type){
            if(type == 'view'){
                this.deliveryman_ids = []
                console.log(this.listIndexArr)
                this.thisAreaDeliveryData = []
                var ctn = 0
                var kgs = 0
                var m3 = 0
                for (var i = 0; i < this.list.length; i++) {
                    if($.inArray(i,this.listIndexArr) >= 0){
                        this.thisAreaDeliveryData.push( this.list[i])
                        ctn += this.list[i].ctn
                        kgs += this.list[i].salePackKgs
                        m3 += this.list[i].volume
                    }
                }
                this.thisAreaDeliveryDataFooter = {
                    ctn:ctn,
                    kgs:kgs,
                    m3:m3,
                }    
                this.getDeliveryman('batchDelivery')
                this.batchSave = true
            }else if(type == 'save'){
                $('#logistor_deliveryModal .alert').hide()
                var deliveryman_idsLenth = this.deliveryman_ids.length
                if(deliveryman_idsLenth == 0){
                    this.point({status:2,msg:'请选择送货人员，不多于3个送货人员'},'#logistor_deliveryModal')
                    return false
                }else if(deliveryman_idsLenth > 3){
                    this.point({status:2,msg:'当前最多只能选择3个送货人员'},'#logistor_deliveryModal')
                    return false
                }else {
                    // 必须安排1个司机
                    if(this.ifIsset(31) == 0){
                        this.point({status:2,msg:'请选择一个司机人选'},'#logistor_deliveryModal')
                        return false
                    }
                }

                this.thisAreaAndCitySave('batchDelivery')
            }
        },

        // 自取完成
        selfFetchingOK(listDeta,type){
            if(type == 'view'){
                this.selfFetchingOKData = listDeta
            }else{
                this.ajaxOK(this.selfFetchingOKData,'selfFetchingOK','#logistor_takeawayModal')
            }
        },
        // 本区 送货完成
        thisAreaOK(listDeta,type){
            if(type == 'view'){
                this.thisAreaAndCityOKData = listDeta
                console.log(this.thisAreaAndCityOKData)
                this.areaOrcity = 'area'
            }else if(type == 'save'){
                this.ajaxOK(this.thisAreaAndCityOKData,'thisAreaAndCityOK','#logistor_delivery_receiveModal')
            }
        },
        // 本市 送货完成
        thisCityOK(listDeta,type){
            if(type == 'view'){
                this.thisAreaAndCityOKData = listDeta
                console.log(this.thisAreaAndCityOKData)
                this.areaOrcity = 'city'
            }else if(type == 'save'){
                this.ajaxOK(this.thisAreaAndCityOKData,'thisAreaAndCityOK','#logistor_delivery_receiveModal')
            }
        },
        // 物流--通知
        logistorNote(listDeta,type){
            if(type == 'view'){
                this.logistorCompany_id = listDeta.trans_id
                this.logistorNoteData = listDeta
                this.getLogistorCompany('logistorNote','main')
                if(this.logistorCompany_id){
                    this.getLogistorCompany('logistorNote','detail')
                }
            }else if(type =='save'){
                var data = {
                    remark:$('#remark').val(),
                    ctn:this.logistorNoteData.ctn,
                    m3:this.logistorNoteData.volume,
                    sale_id:this.logistorNoteData.id,
                    trans_id:this.logistorCompany_id,
                    kgs:this.logistorNoteData.salePackKgs,
                    make_id:this.logistorNoteData.make_id,
                    warehouse_id:this.logistorNoteData.warehouse_id,
                    delivery_note_id:this.logistorNoteData.delivery_note_id,
                }
                this.isdisabled = true
                $.ajax({
                    url:this.requestUrl+'logistorNote?requestType=save',
                    type:'post',
                    data,
                    success:(data)=>{
                        this.isdisabled = false
                        data = JSON.parse(data)
                        this.point(data,'#logistor_callModal');
                        if(data.status == 1){
                            this.getdata()
                            this.point(data,'#mainalert',2);
                        }
                    }
                })
            }
        },
        // 物流--取货
        logistorPickup(listDeta,type){
            // logistoPickup
            if(type == 'view'){

                this.deliveryman_ids = []
                this.logistorPickupData = listDeta
                this.getDeliveryman('logistorPickup')

                this.logistorPickupData = []
                this.logistorPickupData = listDeta
                console.log(this.logistorPickupData)
            }else if(type == 'save'){
                if(!this.allInternDeliveryman()){
                    return false
                }
                this.pickup('logistorPickup')
            }
        },
        // 已安排 -- 物流已取走
        logistorPickupOK(listDeta,type){
            if(type == 'view'){
                this.logistorPickupData = []
                this.logistorPickupData = listDeta
                this.logistorCompany_id = listDeta.trans_id
                this.logistorLinkAndAttr = []
                this.getLogistorCompany('logistorPickupOK','getLogistorCompany')
                this.getDeliveryman('logistorPickupOK')
                
            }else if(type == 'save'){
                if(!$('#logistorNo').val()){
                    this.point({status:2,msg:'请填写物流单号'},'#logistor_call_receiveModal');
                    return false
                }

                var data = {
                    sale_id:this.logistorPickupData.id,
                    make_id:this.logistorPickupData.make_id,
                    teams:this.logistorPickupData.teams,
                    logistorNo:$('#logistorNo').val()
                }
                this.isdisabled = true
                $.ajax({
                    url:this.requestUrl+'logistorPickupOK?requestType=save',
                    type:'post',
                    data,
                    success:(data)=>{
                        this.isdisabled = false
                        data = JSON.parse(data)
                        this.point(data,'#logistor_call_receiveModal');
                        if(data.status == 1){
                            this.getdata()
                            this.point(data,'#mainalert',2);
                        }
                    }
                })
            }
        },
        // 送货人员
        deliverymanBtn(type){
            if(type == 'view'){
                this.deliveryman = []
                this.getDeliveryman('deliverymanBtn')
            }
        },
 





        // 本区 本市 提交 
        thisAreaAndCitySave(path){
            var data = {
                data:this.thisAreaDeliveryData,
                deliveryman_ids:this.deliveryman_ids,
                thisAreaDeliveryDataFooter:this.thisAreaDeliveryDataFooter,
            }
            this.isdisabled = true
            $.ajax({
                url:this.requestUrl+path+'?requestType=save',
                type:'post',
                data,
                success:(data)=>{
                    this.isdisabled = false
                    data = JSON.parse(data)
                    this.point(data,'#logistor_deliveryModal');
                    if(data.status == 1){
                        this.getdata()
                        this.point(data,'#mainalert',2);
                    }
                }
            })
        },
        // 本区 本市 提交 判断送货人员
        iftTams(){
            // 起步安排2人
            
        },
        // 选择物流公司
        selectlogistorCompany(){
            this.logistorLinkAndAttr = []
            if(this.logistorCompany_id > 0){
                this.getLogistorCompany('logistorNote','detail')
            }
        },
        ajaxOK(dataInfo,path,classID){
            var receipt = 0;
            // 是否有回执
            $('input[name="receipt"]:checked').each(function(index, element) {
                receipt = $(this).val();
            });
            var data = {
                teams:dataInfo.teams,
                sale_id:dataInfo.id,
                make_id:dataInfo.make_id,
                delivery_type:dataInfo.delivery_type_id,

                ctn:dataInfo.ctn,
                volume:dataInfo.volume,
                salePackKgs:dataInfo.salePackKgs,
                make_delivery_start_at:dataInfo.make_delivery_start_at,

                receipt:receipt,
                receipt_id:$('#receipt_id').val(),
                areaOrcity:this.areaOrcity
            }
            $.ajax({
                url:this.requestUrl+path+'?requestType=save',
                type:'post',
                data,
                success:(data)=>{
                    data = JSON.parse(data)
                    this.point(data,classID);
                    if(data.status == 1){
                        this.getdata()
                        this.point(data,'#mainalert',2);
                    }
                }
            })
        },
        //判断是否有队长 队长position_id == 37
        ifIsset(key){
            var sum = 0
            var ids = [];
            for (var j = 0; j < this.deliveryman.length; j++) {
                if(this.deliveryman[j].position_id == key) ids.push(this.deliveryman[j].admin_id)
            }
            for (var i = 0; i < this.deliveryman_ids.length; i++) {
                if( $.inArray(this.deliveryman_ids[i],ids) >= 0){
                    sum += 1
                }
            }
            return sum
        },
        pickup(path){
            var data = {
                deliveryman_ids:this.deliveryman_ids,
                sale_id:this.logistorPickupData.id,
                make_id:this.logistorPickupData.make_id,
                warehouse_id:this.logistorPickupData.warehouse_id,
                delivery_note_id:this.logistorPickupData.delivery_note_id,
                delivery_type:this.logistorPickupData.delivery_type_id,

                co_id:this.logistorPickupData.co_branch_id ? this.logistorPickupData.co_branch_id : this.logistorPickupData.co_id,

                ctn:this.logistorPickupData.ctn,
                volume:this.logistorPickupData.volume,
                salePackKgs:this.logistorPickupData.salePackKgs,
            }
            this.isdisabled = true
            $.ajax({
                url:this.requestUrl+path+'?requestType=save',
                type:'post',
                data,
                success:(data)=>{
                    this.isdisabled = false
                    data = JSON.parse(data)
                    this.point(data,'#logistor_takeModal');
                    if(data.status == 1){
                        this.getdata()
                        this.point(data,'#mainalert',2);
                    }
                }
            })
        },
        // 判断是否全是实习送货员 
        allInternDeliveryman(){
            if(this.deliveryman_ids.length > 0){
                if(this.deliveryman_ids.length > 3){
                    this.point({status:2,msg:'选择协助人员不能超过三人'},'#logistor_takeModal');
                    return false
                }else {
                    var sum = 0;
                    for (var i = 0; i < this.deliveryman_ids.length; i++) {
                        if($.inArray(this.deliveryman_ids[i], this.logistorPickupData.Intern_deliveryman) >= 0){
                            sum += 1
                        }
                    }
                    if(this.deliveryman_ids.length == 1 && sum == 1){
                        this.point({status:2,msg:'选择协助人员不能单独选择实习送货员'},'#logistor_takeModal','show');
                        return false
                    }
                    if(sum == this.deliveryman_ids.length){
                        this.point({status:2,msg:'选择协助人员不能全是实习送货员'},'#logistor_takeModal','show');
                        return false
                    }
                    var persionNum = Math.ceil(this.logistorPickupData.volume/1.5);
                    if( persionNum < this.deliveryman_ids.length ){
                        this.point({status:2,msg:'选择协助人员过多，当前最多能选择'+persionNum+'个协助人员'},'#logistor_takeModal','show');
                        return false
                    }
                }
            }
            return true
        },

        getLogistorCompany(path,type){
            $.ajax({
                url:this.requestUrl+path+'?requestType='+type,
                type:'post',
                data:{company_id:this.logistorCompany_id},
                success:(data)=>{
                    data = JSON.parse(data)
                    if(type == 'main'){
                        this.logistorCompany = data
                    } else{
                        this.logistorLinkAndAttr = data
                    }
                }
            })
        },
        // 获取送货员
        getDeliveryman(path){
            $.ajax({
                url:this.requestUrl+path+'?requestType=getDeliveryman',
                type:'post',
                success:(data)=>{
                    data = JSON.parse(data)
                    this.deliveryman = data
                console.log(this.deliveryman)
                }
            })
        },
        clickGetData(type){
            this.requestType = type
            this.getdata()
        },
        //设置popper组件的配置
        setpopper(e){
            $(e.target).popover({
                trigger: 'hover focus manual',
                placement: ($(e.target).attr("placement") == undefined) ? 'left' : $(e.target).attr("placement"),
                //placement:'left',
                title: $(e.target).attr("title"),
                html: 'true',
                content: $(e.target).find(".content").html(),
                animation: false
            });
        },
        point(data,modelId,type=1){
            change_mes_type(modelId+' .alert',data.status)
            if(data.status == 1){
                if(type == 1){
                    if(modelId != '.container-fluid') $(modelId).modal('toggle')
                }
                $(modelId+' .alert').show()
                $(modelId+' .alert p').html('*'+data.msg+'<br>')
            }else{
                $(modelId+' .alert').show()
                $(modelId+' .alert p').html('*'+data.msg+'<br>')
            }
        },
        // 提示数量
        cueCount(){
            $.ajax({
                url:this.requestUrl+'list?requestType=cueCount',
                type:'post',
                data:{is_selfWork:this.is_selfWork},
                success:(data)=>{
                    data = JSON.parse(data)
                    this.sellfCount = data.sellfCount
                    this.areaCount = data.areaCount
                    this.cityfCount = data.cityfCount
                    this.logistorfCount = data.logistorfCount
                    this.deliveryCount = data.deliveryCount
                    this.finishCount = data.finishCount
                }
            })
        },
        // 只显示自己工作 内容
        selfWork(){
            this.is_selfWork = 1
            this.getdata()
        },
        // 查找
        clickCearch(){
            this.getdata()
        },
        // 送货人员--离岗
        deliverymanWorkout(deliveryman_id,deliveryman_name){
            this.isdisabled = true
            $.ajax({
                url:this.requestUrl+'deliverymanWorkout',
                type:'post',
                data:{admin_id:deliveryman_id,admin_name:deliveryman_name},
                success:(data)=>{
                    this.isdisabled = false
                    data = JSON.parse(data)
                    this.point(data,'#deliveryman_editModal');
                    if(data.status == 1){
                        this.getdata()
                        this.point(data,'#mainalert',2);
                    }
                }
            })
        },
        // 送货人员--请假
        deliverymanLeave(deliveryman_id,deliveryman_name){
            this.isdisabled = true
            $.ajax({
                url:this.requestUrl+'deliverymanLeave',
                type:'post',
                data:{admin_id:deliveryman_id,admin_name:deliveryman_name},
                success:(data)=>{
                    this.isdisabled = false
                    data = JSON.parse(data)
                    this.point(data,'#deliveryman_editModal');
                    if(data.status == 1){
                        this.getdata()
                        this.point(data,'#mainalert',2);
                    }
                }
            })
        },
        // 打印
        print(){
            $.ajax({
                url:this.requestUrl+'print',
                type:'post',
                success:(data)=>{
                    // data = JSON.parse(data)
                    // this.point(data,'#deliveryman_editModal');
                    // if(data.status == 1){
                    //     // local.href
                    // }
                }
            })
        }
    },
    mounted(){
        this.requestUrl = "/admin/salelogistor/";
        this.getdata()
        this.cueCount()
        // this.getDeliveryman('list')
    }
})